package antrace;

import processing.core.PImage;
import processing.core.PMIDlet;

import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Layer;

public class GameView implements InterfaceView {

	private PImage frame, tillbakapil;
	private PlayerController controller;
	private LayerManager layerManager;
	private AbstractGameLayer[] gameLayers;
	private int nrGameLayers = 0;
	private AntSprite myAnt;

	public BlockGenerator blockGenerator;

	/* Alla game-layers (läggs sedan in i arrayen gameLayers) */
	private SkyGameLayer skyLayer;
	private ForestGameLayer forestLayer;
	private GroundGameLayer groundLayer;
	private DiggableGameLayer diggableLayer;
	private BonusGameLayer bonusLayer;
	private TongueGameLayer tongueLayer;

	private String filename = "highscore.txt";
	private String[] saveHighscore = new String[5];
	
	public IngameMenuOverlay ingameMenu;
	GameoverOverlay gameoverMenu;
	
	private long gameTimer;

	public GameView() {
		blockGenerator = new BlockGenerator();

		frame = new PImage(240, 320);
		tillbakapil = AntRace.midlet.loadImage("/images/gui/tillbakapil.png");

		myAnt = new AntSprite(AntRace.midlet.loadImage("/images/sprites/ant_sprite03.png"));
		controller = new PlayerController(myAnt);

		gameLayers = new AbstractGameLayer[6];
		layerManager = new LayerManager();

		skyLayer = new SkyGameLayer();
		addLayer(skyLayer);

		forestLayer = new ForestGameLayer();
		addLayer(forestLayer);		

		groundLayer = new GroundGameLayer();
		addLayer(groundLayer);

		diggableLayer = new DiggableGameLayer(myAnt);
		addLayer(diggableLayer);

		bonusLayer = new BonusGameLayer(myAnt);
		addLayer(bonusLayer);

		// myAnt måste läggas in (näst) sist! efter alla banlager
		layerManager.insert(myAnt, 0);

		// Tungan läggs till allra sist, ovanför myran
		tongueLayer = new TongueGameLayer(diggableLayer, myAnt);
		addLayer(tongueLayer);
		
		ingameMenu = new IngameMenuOverlay();
		gameoverMenu = new GameoverOverlay();
	}

	public void resetGame() {
		gameTimer = 0;
		myAnt.resetGame();
		
		for (int i = 0; i < gameLayers.length; i++) {
			if (gameLayers[i] != null) {
				gameLayers[i].reset();
			}
		}
		
		/*// 	nya spellager
		
		layerManager.remove(diggableLayer);
		layerManager.remove(bonusLayer);
		
		diggableLayer = new DiggableGameLayer(myAnt);
		bonusLayer = new BonusGameLayer(myAnt);
		
		layerManager.insert(diggableLayer, 2);
		layerManager.insert(bonusLayer, 2);*/
	}

	public void saveHighscore() {
		saveHighscore = AntRace.midlet.loadStrings(AntRace.FILENAME_HIGHSCORE);
		saveHighscore = HighscoreOverlay.sortHighscore(saveHighscore);
		//		for (int i = 0; i < saveHighscore.length; i++) {
		//			System.out.println("I saveHighscore(): " +saveHighscore[i]);
		//		}	
		//		System.out.println("efter insättning");
		saveHighscore[0] = "NEW   "+myAnt.getScore();
		//		for (int i = 0; i < saveHighscore.length; i++) {
		//			System.out.println("I saveHighscore(): " +saveHighscore[i]);
		//		}
		AntRace.midlet.saveStrings(filename, saveHighscore);
	}
	
	public void setLastFrame(){
		ingameMenu.setLastFrame(frame);
		gameoverMenu.setLastFrame(frame);
	}

	public void draw() {
		if(ingameMenu.isVisible()){
			ingameMenu.draw();
		}
		else if(gameoverMenu.isVisible()){
			gameoverMenu.draw();
		}
		else{
			gameTimer++;
			frame = new PImage(240, 320);
			
			if (gameTimer % (5*20) == 0) {
				tongueLayer.increaseLength(1);
			}
			
			// var 4:e sekund kommer tungan (4sek*20fps)
			if (gameTimer % (2*20) == 0) {
				tongueLayer.launch();
			}
	
			if (gameTimer % 20 == 0 && !myAnt.isDead()) {
				myAnt.addScore(1);
			}
	
			controller.update();
			this.updateLayers();
			this.collideLayers();
			myAnt.update();
			this.scrollLayers();
	
			layerManager.paint(frame.image.getGraphics(), 0, 0);
	
			AntRace.midlet.image(frame,0,0);
			
			AntRace.midlet.textFont(AntRace.font);
			AntRace.midlet.textAlign(PMIDlet.RIGHT);
			AntRace.midlet.text(PMIDlet.str(myAnt.getScore()),230,20);
			AntRace.midlet.textAlign(PMIDlet.LEFT);
			
			AntRace.midlet.image(tillbakapil, 20, 295);
		}
	}

	public void addLayer(AbstractGameLayer newLayer) {
		layerManager.insert((Layer)newLayer, 0);
		gameLayers[nrGameLayers] = newLayer;
		nrGameLayers++;
	}

	public void updateLayers() {
		for (int i = 0; i < gameLayers.length; i++) {
			if (gameLayers[i] != null) {
				gameLayers[i].update();

			}
		}
	}

	public void collideLayers() {
		for (int i = 0; i < nrGameLayers; i++) {
			if (myAnt.collidesWith(gameLayers[i], false)) {
				gameLayers[i].collide(myAnt);
			}
		}
	}

	public void scrollLayers() {
		int delta = myAnt.getDelta_X();

		if( myAnt.getWorldX() < myAnt.fixedCenterPositionX) {
			delta = 0;
			groundLayer.reset();
			diggableLayer.reset();
			bonusLayer.reset();
		}

		for (int i = 0; i < nrGameLayers; i++) {
			gameLayers[i].scroll(delta);
		}
	}

	public AntSprite getAnt() {
		return myAnt;
	}

	public void keyPressed(int keyCode) {
		if(ingameMenu.isVisible())
			ingameMenu.keyPressed(keyCode);
		else
			controller.keyPressed(keyCode);
	}

	public void keyReleased(int keyCode) {
		if(ingameMenu.isVisible())
			ingameMenu.keyReleased(keyCode);
		else
			controller.keyReleased(keyCode);
	}

}
